﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	PointDialog
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Map_Fvg
{
    public partial class PointDialog : Form
    {
        public PointDialog()
        {
            InitializeComponent();

            dataGridView1.ColumnCount = 6;
            this.dataGridView1.Columns[0].HeaderText = "codice";
            this.dataGridView1.Columns[1].HeaderText = "denominazione";
            this.dataGridView1.Columns[2].HeaderText = "simbolo";
            this.dataGridView1.Columns[3].HeaderText = "parametri";
            this.dataGridView1.Columns[4].HeaderText = "file .bmp";
            this.dataGridView1.Columns[5].HeaderText = "dimensione";
            //           this.dataGridView1.Columns[4].HeaderText = "visib";

            this.dataGridView1.Columns[0].Width = 70;
            this.dataGridView1.Columns[1].Width = 300;
            this.dataGridView1.Columns[2].Width = 60;
            this.dataGridView1.Columns[3].Width = 110;
            this.dataGridView1.Columns[4].Width = 70;
            this.dataGridView1.Columns[5].Width = 70;

            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;

            int iSel = 0;
            for (int i = 0; i < ViewQ.nSimbP; ++i)
            {
                string[] row = new string[] {
                ViewQ.vectP[i].codice.ToString(),ViewQ.vectP[i].denom.ToString()};
                dataGridView1.Rows.Add(row);
                dataGridView1[2, iSel].Value = ViewQ.vectP[i].simbolo.ToString();
                dataGridView1[3, iSel].Value = ViewQ.vectP[i].paramSimb;
                if (string.IsNullOrEmpty(ViewQ.vectP[i].fileName)) dataGridView1[4, iSel].Value = "  ";
                else
                {
                    dataGridView1[4, iSel].Value = ViewQ.vectP[i].fileName;
                }
                dataGridView1[5, iSel].Value = ViewQ.vectP[i].dimensione;

                ++iSel;
            }

            textBox1.Text = iSel.ToString();

            button1.DialogResult = DialogResult.Cancel;
            button2.DialogResult = DialogResult.OK;
            DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
            {
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
                column.FlatStyle = FlatStyle.Standard;
                column.ThreeState = false;
                column.CellTemplate = new DataGridViewCheckBoxCell();
                column.CellTemplate.Style.BackColor = Color.White;
                column.HeaderText = "visibile";
            }
            int iCol = 6;
            dataGridView1.Columns.Insert(iCol, column);

            
            iSel = 0;
            for (int i = 0; i < ViewQ.nSimbP; ++i)
            {
                if (ViewQ.vectP[i].selected == 0) continue;

                if (ViewQ.vectP[i].visib == 0) dataGridView1[iCol, iSel].Value = false;
                else
                {
                    dataGridView1[iCol, iSel].Value = true;
                }
                ++iSel;
            }

            DataGridViewCheckBoxColumn column1 = new DataGridViewCheckBoxColumn();
            {
                 column1.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
                 column1.FlatStyle = FlatStyle.Standard;
                 column1.ThreeState = false;
                 column1.CellTemplate = new DataGridViewCheckBoxCell();
                 column1.CellTemplate.Style.BackColor = Color.White;
                 column1.HeaderText = "presente in archivio";
            }

            iCol = 7;
            dataGridView1.Columns.Insert(iCol, column1);

            using (SQLiteConnection conn = SetConnection())
            {
                 conn.Open();
                 for (int i = 0; i < ViewQ.nSimb; ++i)
                 {
                      if (ViewQ.vectP[i].selected == 0) continue;

                      using (SQLiteCommand cmd = new SQLiteCommand(conn))
                      {
                           cmd.CommandText = "select * from point where codice_point = '" + ViewQ.vectP[i].codice.ToString() + "'";

                           SQLiteDataReader reader = cmd.ExecuteReader();
                          if (reader.HasRows) dataGridView1[iCol, i].Value = true;
                      }
                 }
                 conn.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int count = dataGridView1.Rows.Count;

            for (int i = 0; i < count; ++i)
            {
                string row = dataGridView1[0, i].Value.ToString();
                for (int j = 0; j < ViewQ.nSimbP; ++j)
                {
                    int comp = string.Compare(ViewQ.vectP[i].elemento, 0, ViewQ.selectedElement, 0, 6);
                    comp = 0;
                    if (comp == 0)
                    {
                        if (ViewQ.vectP[j].codice.Contains(row))
                        {
                            ViewQ.vectP[j].simbolo = int.Parse(dataGridView1[2, i].Value.ToString());
                            ViewQ.vectP[j].paramSimb = dataGridView1[3, i].Value.ToString();
                            ViewQ.vectP[j].fileName = dataGridView1[4, i].Value.ToString();
                            if ((bool)dataGridView1[6, i].Value) ViewQ.vectP[j].visib = 1;
                            else ViewQ.vectP[j].visib = 0;
                            ViewQ.vectP[j].dimensione = float.Parse(dataGridView1[5, i].Value.ToString());
                            break;
                        }
                    }
                }
            }

            this.Close();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            int count = this.dataGridView1.RowCount;
            bool bChecked = (bool)checkBox1.Checked;
            if (bChecked)
            {
                for (int j = 0; j < count; ++j)
                {
                    dataGridView1[6, j].Value = false;
                }
            }
            else
            {
                for (int j = 0; j < count; ++j)
                {
                    dataGridView1[6, j].Value = true;
                }
            }

            dataGridView1.Refresh();

        }

 

        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }

        private string aggiungiApostrofo(string orig)
        {
            string s1 = "";
            string s2 = ""; string s3 = "";
            int ipos = 0, len = 0;
            int ipos1 = 0;
            s2 = orig;

        L10: ipos = s2.IndexOf('\x27', ipos1);

            if (ipos > 0)
            {
                len = s2.Length;
                s1 = s2.Substring(0, ipos);
                s1 = string.Concat(s1, "\x27\x27");
                s3 = s2.Substring(ipos + 1, len - ipos - 1);
                s1 = string.Concat(s1, s3);
                s2 = s1;
                ipos1 = ipos + 2;
                goto L10;
            }

            return (s2);
        }




    }
}
